import BTComposite from "../../Base/BTComposite";
import { NodeStatus } from "../../Enum";

export default class CompositeSelector extends BTComposite
{
    onStart()
    {
        super.onStart();
        this.index = 0;
    }

    canExecute()
    {
        return this.index < this.childens.length && this.status !== NodeStatus.Success;
    }

    onChildExecuted(childStatus)
    {
        switch (childStatus)
        {
            case NodeStatus.Success:
                this.status = NodeStatus.Success;
                break;
            case NodeStatus.Failure:
                this.index++;
                if (this.index >= this.childens.length)
                {
                    this.status = NodeStatus.Failure;
                } else
                {
                    this.status = NodeStatus.Running;
                }
                break;
            case NodeStatus.Running:
                this.status = NodeStatus.Running;
                break;
            default:
                break;
        }
    }

    onConditionalAbort(index: number)
    {
        this.index = index;
        this.status = NodeStatus.Inactive;
    }

/*    onUpdate()
    {
        if (this.status === NodeStatus.Success)
        {
            return this.status;
        }
        if (this.index >= this.childens.length)
        {
            this.status = NodeStatus.Failure;
            return this.status;
        }

        const child = this.childens[this.index];
        const res = child.run();
        if (res === NodeStatus.Success)
        {
            this.status = NodeStatus.Success;
            return this.status;
        }
        if (res === NodeStatus.Failure)
        {
            this.index++;
        }
        return NodeStatus.Running;
    }*/
}